package com.bnz.shop.controller;

;
import com.bnz.shop.entity.ItemCatEntity;
import com.bnz.shop.service.ItemCatService;
import com.bnz.utils.PageUtils;
import com.bnz.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 商品类目
 *
 * @author zsl
 * @email 1966628784@qq.com
 * @date 2022-09-19 12:45:05
 */
@RestController
@RequestMapping("itemcat")
public class ItemCatController {
    @Autowired
    private ItemCatService itemCatService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = itemCatService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") String id){
		ItemCatEntity itemCat = itemCatService.getById(id);

        return R.ok().put("itemCat", itemCat);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody ItemCatEntity itemCat){
		itemCatService.save(itemCat);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody ItemCatEntity itemCat){
		itemCatService.updateById(itemCat);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] ids){
		itemCatService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 根据parentid查询出分类
     * 父类（分类的一级）
     */
    @GetMapping("findByParentId/{parentId}")
    public R findByParentId(@PathVariable("parentId") String parentId){
        List<ItemCatEntity> byParentId = itemCatService.findByParentId(parentId);
        return R.ok().put("list",byParentId); // “list”为前端设置的对接参数
    }
}

